package com.amin.app.web.controller;

import com.amin.app.web.entity.Page;
import com.amin.app.web.entity.SDictList;
import com.amin.app.web.entity.SMenu;
import com.amin.app.web.entity.SRole;
import com.amin.app.web.service.DictListService;
import com.amin.app.web.service.MenuSerivce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MenuController {
    final
    MenuSerivce menuSerivce;
    final
    DictListService dictListService;

    public MenuController(MenuSerivce menuSerivce, DictListService dictListService) {
        this.menuSerivce = menuSerivce;
        this.dictListService = dictListService;
    }

    /**
     * 获取菜单列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/menu/getMenuList")
    public String getMenuList(Model model, Page page, SMenu sMenu){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SMenu> menuList = menuSerivce.getMenuList(sMenu);
        List<SMenu> menuButtonList = menuSerivce.getMenuButtonListByMenuId(sMenu.getMenuId());
        PageInfo<SMenu> menuListPage = new PageInfo<>(menuList);
        model.addAttribute("menuList",menuListPage);
        model.addAttribute("menuButtonList",menuButtonList);
        model.addAttribute("menuId",sMenu.getMenuId());
        model.addAttribute("menu",sMenu);
        //菜单类型
        List<SDictList> sys_menu_type = dictListService.getDictListByDictType("sys_menu_type");
        model.addAttribute("systemMenuType", sys_menu_type);
        //状态
        List<SDictList> sys_switch_type = dictListService.getDictListByDictType("sys_switch_type");
        model.addAttribute("systemSwitchType", sys_switch_type);
        //菜单跳转方式
        List<SDictList> sys_menu_jump_type = dictListService.getDictListByDictType("sys_menu_jump_type");
        model.addAttribute("systemMenuJumpType", sys_menu_jump_type);

        return "amin/menu/menuList";
    }

    /**
     * 根据角色id获取菜单列表
     * @param model
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/menu/getMenuListByRoleId")
    public String getMenuListByRoleId(Model model,Integer roleId){
        return  "";
    }

    /**
     * 创建菜单
     * @param model
     * @param sMenu
     * @return
     */
    @RequestMapping(value = "/menu/createMenu")
    public String createMenu(Model model, SMenu sMenu){
        //菜单类型
        List<SDictList> sys_menu_type = dictListService.getDictListByDictType("sys_menu_type");
        model.addAttribute("systemMenuType", sys_menu_type);
        List<SMenu> menuList = menuSerivce.getMenuListByMenu();
        model.addAttribute("menuList",menuList);
        sMenu.setMenuType("1");
        if (null != sMenu.getId() && 0 != sMenu.getId()){
            sMenu = menuSerivce.getMenuById(sMenu.getId());
        }
        model.addAttribute("menu",sMenu);
        return "/amin/menu/createMenu";
    }

    /**
     * 数据提交
     * @param sMenu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/menu/dataSubmit")
    public Map<Object,Object> dataSubmit(SMenu sMenu){
        Map<Object,Object> answer = new HashMap<>();
        try {
            if (null == sMenu.getId() || 0 ==  sMenu.getId()){
                sMenu.setCreatedTime(new Date());
               menuSerivce.save(sMenu);
            }else {
                menuSerivce.update(sMenu);
            }
            answer.put("code",0);
            answer.put("msg","数据保存成功");
        }catch (SqlSessionException e){
            answer.put("code",1);
            answer.put("msg",e.getMessage());
        }
        return answer;
    }

    /**
     * 删除菜单
     * @param model
     * @param sMenu
     * @return
     */
    @RequestMapping(value = "/menu/delById")
    public String delById(Model model, SMenu sMenu){
        menuSerivce.delById(sMenu.getId());
        return "redirect:/menu/getMenuList";
    }
}
